package com.vito.framework.system.operatelog.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vito.framework.base.controller.BaseController;
import com.vito.framework.base.exception.CommonException;
import com.vito.framework.system.operatelog.model.OperateLogBean;
import com.vito.framework.system.operatelog.service.OperateLogService;
import com.vito.support.ApiResponse;
import com.vito.support.ResultEnum;
import com.vito.util.excel.ExportBean;
import com.vito.util.excel.ExportExcelJxl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/***************************************************************
 * Name:        OperateLogAction.java     
 * Create At:   2012-05-13
 * Module:      系统管理
 * Description: [系统管理-操作日志查询]
 * Author       miaowenqiang
 * ------------------------------------------------------------
 * 修改历史
 * 序号  日期  修改人   修改原因
 * 1
 * 2
 ***************************************************************/

@RestController
@RequestMapping("${adminPath}/operateLog")
public class OperateLogController extends BaseController {

    @Autowired
    private OperateLogService operateLogService;


    @PostMapping(value = "queryList", consumes = "application/json;charset=UTF-8")
    public ApiResponse queryList(HttpServletRequest request, @RequestBody JSONObject params) {
        Map<String, Object> paramMap = super.getParamMap(request, params);
        if (null == paramMap) {
            return ApiResponse.of(ResultEnum.PARAMS_INVALID);
        }

        int page = params.getIntValue("page");
        int limit = params.getIntValue("limit");

        paramMap.put("param_operateType", params.getString("param_operateType"));
        paramMap.put("param_startDate", params.getString("param_startDate"));
        paramMap.put("param_endDate", params.getString("param_endDate"));
        Page<OperateLogBean> pageCount = new Page<>(page, limit);
        return ApiResponse.ofPage(operateLogService.queryOperateLogList(pageCount, paramMap));
    }

    /**
     * Author:miaowenqiang
     * Create At:2012-5-11
     * Description: [导出操作日志信息页面]
     *
     * @return String
     * @throws Exception
     */
    @PostMapping(value = "exportOperateLog", consumes = "application/json;charset=UTF-8")
    public ApiResponse exportOperateLog(HttpServletRequest request, @RequestBody JSONObject params, HttpServletResponse response) {
        Map<String, Object> paramMap = super.getParamMap(request, params);
        if (null == paramMap) {
            return ApiResponse.of(ResultEnum.PARAMS_INVALID);
        }

        paramMap.put("param_operateType", params.getString("param_operateType"));
        paramMap.put("param_startDate", params.getString("param_startDate"));
        paramMap.put("param_endDate", params.getString("param_endDate"));

        try {
            Page<OperateLogBean> page = new Page<>(0, 20000);
            List<OperateLogBean> resultList = operateLogService.queryOperateLogList(page, params).getRecords();
            int len = resultList.size();
            Object[][] data = new Object[len + 1][15];
            data[0] = new Object[]{"流水号", "菜单名称", "操作描述", "操作时间", "操作人", "操作人帐号", "IP地址", "操作类型"};

            int i = 1;
            for (OperateLogBean olb : resultList) {
                data[i][0] = olb.getOperateId();
                if (olb.getMenuId() == null || "".equals(olb.getMenuId())) {
                    data[i][1] = "登录";
                } else {
                    data[i][1] = olb.getMenuId();
                }
                data[i][2] = olb.getRemark();
                data[i][3] = olb.getOpdate();
                data[i][4] = olb.getOperator();
                data[i][5] = olb.getOperatorid();
                data[i][6] = olb.getIp();
                data[i][7] = olb.getOperateType();
                i++;
            }

            ExportBean excelBean = new ExportBean();
            excelBean.setData(data);
            excelBean.setExcelName("操作日志信息");
            excelBean.setSheetName("操作日志信息");
            excelBean.setExportMode(0);
            new ExportExcelJxl(response, excelBean);
        } catch (CommonException e) {
            return ApiResponse.of(e);
        }
        return null;
    }

    /**
     * Author:miaowenqiang
     * Create At:2012-5-11
     * Description: [导出操作日志信息页面]
     *
     * @return String
     * @throws Exception
     */
    @PostMapping(value = "deleteOperateLog", consumes = "application/json;charset=UTF-8")
    public ApiResponse deleteOperateLog(HttpServletRequest request, @RequestBody JSONObject params) {
        Map<String, Object> paramMap = super.getParamMap(request, params);
        if (null == paramMap) {
            return ApiResponse.of(ResultEnum.PARAMS_INVALID);
        }

        try {
            operateLogService.remove(new LambdaQueryWrapper<OperateLogBean>().eq(OperateLogBean::getOrgId, paramMap.get("cur_orgId")));
            return ApiResponse.ok(ResultEnum.OK);
        } catch (CommonException e) {
            return ApiResponse.of(e);
        }
    }

}
